/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2021-04-26     yaoyufan       the first version
 */
#include "datadeal.h"



void SSD1306_Thread_Entry(void *parameter)
{
    uint8_t hms_time[8];
    uint8_t ymd_time[10];
    ssd1306_Init();
    ssd1306_Fill(Black);

    ssd1306_SetCursor(45, 20);
    ssd1306_WriteString("Hi~", Font_16x26, White);
    ssd1306_UpdateScreen();
    rt_thread_mdelay(2000);
    ssd1306_Fill(Black);

    ssd1306_SetCursor(20, 0);
    ssd1306_WriteString("Entrance", Font_11x18, White);
    ssd1306_SetCursor(30, 20);
    ssd1306_WriteString("System", Font_11x18, White);
    ssd1306_SetCursor(10, 50);
    ssd1306_WriteString("Designed by YYF", Font_7x10, White);
    ssd1306_UpdateScreen();
    rt_thread_mdelay(2000);
    ssd1306_Fill(Black);

    while(1)
    {
        RTC_Get();
        memset(&hms_time, 0, sizeof(hms_time));
        memset(&ymd_time, 0, sizeof(ymd_time));

        sprintf(&ymd_time,"%04d-%02d-%02d", rtc_time.year, rtc_time.month, rtc_time.day);
        ssd1306_SetCursor(13, 0);
        ssd1306_WriteString(&ymd_time, Font_11x18, White);
        memset(&ymd_time, 0, sizeof(ymd_time));
        sprintf(&hms_time, "%02d:%02d:%02d", rtc_time.hour, rtc_time.min,rtc_time.sec);
        ssd1306_SetCursor(25, 20);
        ssd1306_WriteString(&hms_time, Font_11x18, White);
        ssd1306_UpdateScreen();
        rt_thread_mdelay(1000);
    }
}

void timeout_callback(void *parameter)
{
    uint8_t pin_r = rt_pin_get(LED_R);
    uint8_t pin_g = rt_pin_get(LED_G);
    uint8_t pin_b = rt_pin_get(LED_B);
    switch((uint32_t)parameter)
    {
        case 1:
            rt_pin_write(pin_r, OFF);
            rt_pin_write(pin_g, OFF);
            rt_pin_write(pin_b, OFF);
            break;
        case 2:
            ssd1306_SetCursor(50, 40);
            ssd1306_WriteString("   ", Font_11x18, White);
            break;
    }
}


//终端删除所有卡
void delete_all_card(void)
{
    Delete_AllCard();
    rt_kprintf("Delete All Cards Success!\n");
}
MSH_CMD_EXPORT(delete_all_card, delete all card);

//终端增加管理员卡
void add_manage_card(int argc, char**argv)
{
    uint8_t *p;
    uint8_t Snr[4];
    uint8_t res = -1;

    if(argc < 2)
    {
        rt_kprintf("Please input as add_manage_card 80f730f5\n");
    }
    if(strlen(argv[1]) != 8)
    {
        rt_kprintf("Please input 8 digits hex manage card number!\n");
    }else {
        p = argv[1];
        Str_to_Hex(p, Snr);
        res = Add_ManageCard(Snr);
        if(res == TRUE)
        {
            rt_kprintf("Add Manage Card Success!\n");
        }else {
            rt_kprintf("Add Manage Card Fail!\n");
        }

    }

}
MSH_CMD_EXPORT(add_manage_card, add manage card);

//终端增加普通卡
void add_common_card(int argc, char**argv)
{
    uint8_t *p;
    uint8_t Snr[4];
    uint8_t res = -1;

    if(argc < 2)
    {
        rt_kprintf("Please input as add_common_card 80f730f5\n");
    }
    if(strlen(argv[1]) != 8)
    {
        rt_kprintf("Please input 8 digits hex common card number!\n");
    }else {
        p = argv[1];
        Str_to_Hex(p, Snr);
        res = Add_CommonCard(Snr);
        if(res == TRUE)
        {
            rt_kprintf("Add Common Card Success!\n");
        }else {
            rt_kprintf("Add Common Card Fail!\n");
        }
    }
}
MSH_CMD_EXPORT(add_common_card, add common card);

//终端删除普通卡
void delete_common_card(int argc, char**argv)
{
    uint8_t *p;
    uint8_t Snr[4];
    uint8_t res = -1;

    if(argc < 2)
    {
        rt_kprintf("Please input as delete_common_card 80f730f5\n");
    }
    if(strlen(argv[1]) != 8)
    {
        rt_kprintf("Please input 8 digits hex common card number!\n");
    }else {
        p = argv[1];
        Str_to_Hex(p, Snr);
        res = Delete_Card(Snr, COMMON);
        if(res == TRUE)
        {
            rt_kprintf("Delete Common Card Success!\n");
        }else {
            rt_kprintf("Delete Common Card Fail!\n");
        }
    }
}
MSH_CMD_EXPORT(delete_common_card, delete common card);

//终端删除管理员卡
void delete_manage_card(int argc, char**argv)
{
    uint8_t *p;
    uint8_t Snr[4];
    uint8_t res = -1;

    if(argc < 2)
    {
        rt_kprintf("Please input as delete_manage_card 80f730f5\n");
    }
    if(strlen(argv[1]) != 8)
    {
        rt_kprintf("Please input 8 digits hex manage card number!\n");
    }else {
        p = argv[1];
        Str_to_Hex(p, Snr);
        res = Delete_Card(Snr, MANAGE);
        if(res == TRUE)
        {
            rt_kprintf("Delete Manage Card Success!\n");
        }else {
            rt_kprintf("Delete Manage Card Fail!\n");
        }
    }
}
MSH_CMD_EXPORT(delete_manage_card, delete manage card);

//终端设置通行时间
void set_opentime(int argc, char**argv)
{
    uint8_t Allowtime[4];
    uint8_t *p;

    if(argc < 2)
    {
        rt_kprintf("Please input set_opentime hex as 12001800\n");
    }
    if(strlen(argv[1]) != 8)
    {
        rt_kprintf("Please input 8 digits hex number!\n");
    }else {
        p = argv[1];
        Str_to_Hex(p, Allowtime);
        Set_ComCardTime(Allowtime);
        rt_kprintf("Set Open Time Success!\n");
    }
}
MSH_CMD_EXPORT(set_opentime, set open time);

//终端读取通行时间
void read_opentime(void)
{
    uint8_t Allowtime[4];
    Read_ComCardTime(Allowtime);
    rt_kprintf("Opentime:%02d:%02d-%02d:%02d\n", Allowtime[0], Allowtime[1], Allowtime[2], Allowtime[3]);
}
MSH_CMD_EXPORT(read_opentime, read_opentime);

//终端查询是否为管理员卡
void search_is_manage_card(int argc, char**argv)
{
    uint8_t Snr[4];
    uint8_t *p;
    uint8_t res = -1;

    if(argc < 2)
    {
        rt_kprintf("Please input manage card hex as 80f730f5\n");
    }
    if(strlen(argv[1]) != 8)
    {
        rt_kprintf("Please input 8 digits hex manage card number!\n");
    }else {
        p = argv[1];
        Str_to_Hex(p, Snr);
        res = Search_Card(Snr, MANAGE);
        if(res == TRUE)
        {
            rt_kprintf("This Card is Manage Card!\n");
        }else {
            rt_kprintf("This Card is not Manage Card!\n");
        }
    }
}
MSH_CMD_EXPORT(search_is_manage_card, whether card is manage card);

//终端查询是否为普通卡
void search_is_common_card(int argc, char**argv)
{
    uint8_t Snr[4];
    uint8_t *p;
    uint8_t res = -1;

    if(argc < 2)
    {
        rt_kprintf("Please input common card hex as 80f730f5\n");
    }
    if(strlen(argv[1]) != 8)
    {
        rt_kprintf("Please input 8 digits hex common card number!\n");
    }else {
        p = argv[1];
        Str_to_Hex(p, Snr);
        res = Search_Card(Snr, COMMON);
        if(res == TRUE)
        {
            rt_kprintf("This Card is Common Card!\n");
        }else {
            rt_kprintf("This Card is not Common Card!\n");
        }
    }
}
MSH_CMD_EXPORT(search_is_common_card, whether card is common card);

//将字符转换成16进制卡号
//*p---终端输入的字符
//Snr[]---存放卡号/通行时间
void Str_to_Hex(char *p, char Snr[])
{
    uint8_t str1[2],str2[2];
    uint8_t str3[2],str4[2];

    sprintf(str1, "%c%c", p[0],p[1]);
    sscanf(str1, "%x", &Snr[0]);

    sprintf(str2, "%c%c", p[2],p[3]);
    sscanf(str2, "%x", &Snr[1]);

    sprintf(str3, "%c%c", p[4],p[5]);
    sscanf(str3, "%x", &Snr[2]);

    sprintf(str4, "%c%c", p[6],p[7]);
    sscanf(str4, "%x", &Snr[3]);
}
